<!--********************************************************************
* Copyright© 2000 - 2025 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title data-i18n="resources.title_queryByDistance"></title>
    <style type="text/css">
        body {
            margin: 0;
            overflow: hidden;
            background: #fff;
            width: 100%;
            height: 100%
        }

        #map {
            position: absolute;
            width: 100%;
            height: 100%;
        }

        #toolbar {
            position: absolute;
            top: 50px;
            right: 10px;
            text-align: center;
            z-index: 100;
            border-radius: 4px;
        }
    </style>
</head>
<body>
<div id="toolbar" class="panel panel-primary">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.title_queryByDistance"></h5></div>
    <div class='panel-body content'>
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_query" onclick="queryByDistance()"/>&nbsp;
        <input type="button" class="btn btn-default" data-i18n="[value]resources.text_input_value_clear" onclick="clearFeatures()"/>
    </div>
</div>
<div id="map"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script>
    var local, map, layer, markerLayer1, markerLayer2,
        centerPoint = new SuperMap.Geometry.Point(121, 31),//构造一个点的几何图形
        host = window.isLocal ? window.server : "https://iserver.supermap.io",
        url = host + "/iserver/services/map-world/rest/maps/World";
    init();

    function init() {
        map = new SuperMap.Map("map", {
            controls: [
                new SuperMap.Control.ScaleLine(),
                new SuperMap.Control.Zoom(),
                new SuperMap.Control.Navigation({
                    dragPanOptions: {
                        enableKinetic: true
                    }
                })]
        });
        map.addControl(new SuperMap.Control.LayerSwitcher(), new SuperMap.Pixel(42, 80));
        layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {
            transparent: true,
            cacheEnabled: true
        }, {maxResolution: "auto"});
        layer.events.on({"layerInitialized": addLayer});
        markerLayer1 = new SuperMap.Layer.Markers("Markers");
        markerLayer2 = new SuperMap.Layer.Markers("Markers");

        //在地图上具体位置上画一个图形
        var point = centerPoint,
            size = new SuperMap.Size(44, 40),
            offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
            icon = new SuperMap.Icon("./images/marker-gold.png", size, offset);
        markerLayer1.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
    }

    function addLayer() {
        map.addLayers([layer, markerLayer1, markerLayer2]);
        map.setCenter(new SuperMap.LonLat(106.3, 35), 2);
    }

    function queryByDistance() {
        markerLayer2.clearMarkers();

        var queryByDistanceParams = new SuperMap.REST.QueryByDistanceParameters({
            queryParams: new Array(new SuperMap.REST.FilterParameter({name: "Capitals@World.1"})),
            returnContent: true,
            distance: 30,
            geometry: centerPoint
        });

        var queryByDistanceService = new SuperMap.REST.QueryByDistanceService(url);
        queryByDistanceService.events.on({
            "processCompleted": processCompleted,
            "processFailed": processFailed
        });
        queryByDistanceService.processAsync(queryByDistanceParams);
    }

    function processCompleted(queryEventArgs) {
        var i, j, result = queryEventArgs.result;
        for (i = 0; i < result.recordsets.length; i++) {
            for (j = 0; j < result.recordsets[i].features.length; j++) {
                var point = result.recordsets[i].features[j].geometry,
                    size = new SuperMap.Size(44, 33),
                    offset = new SuperMap.Pixel(-(size.w / 2), -size.h),
                    icon = new SuperMap.Icon("./images/marker.png", size, offset);
                markerLayer2.addMarker(new SuperMap.Marker(new SuperMap.LonLat(point.x, point.y), icon));
            }
        }
    }

    function processFailed(e) {
        widgets.alert.showAlert(e.error.errorMsg, false);
    }

    function clearFeatures() {
        //清除上次的显示结果
        markerLayer2.clearMarkers();
    }

</script>

</body>
</html>